AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

local rel = "weapons/ar2/fire1.wav"
util.PrecacheSound(rel)
include('shared.lua')
local Ground = 1 + 0 + 2 + 8 + 32

function ENT:Initialize()
	self.Entity:SetModel(  "models/props_lab/tpplugholder_single.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType(  MOVETYPE_VPHYSICS   )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	self.Entity:SetName("flak_blaster_1000")
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
	end
	self.nextfire = CurTime()
	self.fire = false
	table.insert(servertable["ship"]["weapons"],self.Entity)
end

function ENT:SpawnFunction( ply, tr )
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 36
	local ent = ents.Create( "flak_blaster_1000" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:OnRemove()
end

function ENT:Think()
	if self.core then
		if  (self.core:IsValid()) then
			if self.use then self.core.turrets = self.core.turrets - 1 self.use = false end
			if self.fire then
				if (self.nextfire < CurTime()) then
					if (self.core.turrets < self.core.maxturrets) then
						if (self.core.bullets > 1)then
							if (self.core.capaciator > 30)then
								if Fieldoffire(self.Entity,120) then
									self.core.capaciator = self.core.capaciator -30
									self.core.bullets = self.core.bullets -1
									self.core.turrets = self.core.turrets + 1
									self.use = true	
									local Startpos =  self.Entity:GetPos() + (self.correct * 50) 
									bullet = {} 
									bullet.Src		= self.Entity:GetPos() + self.correct*50
									bullet.Dir		= self.correct 
									bullet.Spread		= Vector(0.05,0.05,0.05) 
									bullet.Num		= 1 
									bullet.Damage		= 150 
									bullet.Force		= 2 
									bullet.Tracer		= 1	 
									bullet.TracerName	= "Tracer"
									self.Entity:FireBullets( bullet )
									self:EmitSound(rel,500,math.random(200,250))
									self.nextfire = CurTime() + 0.05
								end
							end
						end
					end
				end
			end	
		else
			self.core = nil
		end		
	else
		self.core = getshipcore(self.Entity)
		if not self.core then
			self.Entity:NextThink( CurTime() + 5 )
			return true
		end
	end
end	

function ENT:OnTakeDamage(info)
	Shieldhit(info,self.Entity)
end